package jm.nj.day10.demo04;

import java.io.*;
import java.util.ArrayList;

/*
*
* */
public class Demo02 {
    public static void main(String[] args) throws IOException {
        //把ArrayList集合的字符串存储到文本文件中    张三李四王五
        ArrayList<String> list = new ArrayList<String>();
        list.add("张三");
        list.add("李四");
        list.add("王五");

        //遍历元素 获取元素 写入
        BufferedWriter bw = new BufferedWriter(new FileWriter("Demo02.txt"));

        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            bw.write(s);
            bw.newLine();
            bw.flush();

        }
        bw.close();


        //从文本文件读取数据到ArrayList集合中,并遍历集合

        //创建一个新集合来存储数据
        ArrayList<String> listq = new ArrayList<String>();
        //读取
        BufferedReader br = new BufferedReader(new FileReader("Demo02.txt"));


        //一次一行
        String line;
        while ((line = br.readLine())!=null){
            listq.add(line);

        }
        br.close();
        //遍历集合
        for (int i = 0; i < listq.size(); i++) {
            System.out.println(list.get(i));
        }

    }
}
